﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarSF.StudentInfosManager.ValidationRules
{
    public abstract class LValidationRule : ValidationRule
    {
        /// <summary>
        /// 全角字符半角化。
        /// </summary>
        /// <param name="origin">源文本</param>
        /// <returns>返回转换后的文本。</returns>
        public static string FullCharsToHalf(string origin)
        {
            if (string.IsNullOrEmpty(origin)) return "";

            var chars = new char[origin.Length];
            for (int i = origin.Length - 1; i >= 0; i--)
            {
                var c = origin[i];

                switch (c)
                {
                    //半角标点原样输出
                    case '_': { chars[i] = '_'; continue; }
                    case ',': { chars[i] = ','; continue; }
                    case '.': { chars[i] = '.'; continue; }
                    case '{': { chars[i] = '{'; continue; }
                    case '}': { chars[i] = '}'; continue; }
                    case '[': { chars[i] = '['; continue; }
                    case ']': { chars[i] = ']'; continue; }
                    case '!': { chars[i] = '!'; continue; }
                    case '@': { chars[i] = '@'; continue; }
                    case '#': { chars[i] = '#'; continue; }
                    case '%': { chars[i] = '%'; continue; }
                    case '&': { chars[i] = '&'; continue; }
                    case '*': { chars[i] = '*'; continue; }
                    case '(': { chars[i] = '('; continue; }
                    case ')': { chars[i] = ')'; continue; }
                    case '-': { chars[i] = '-'; continue; }
                    case '=': { chars[i] = '='; continue; }
                    case '+': { chars[i] = '+'; continue; }
                    case '~': { chars[i] = '~'; continue; }
                    case '?': { chars[i] = '?'; continue; }
                    case ';': { chars[i] = ';'; continue; }
                    case ':': { chars[i] = ':'; continue; }
                    case '\'': { chars[i] = '\''; continue; }
                    case '\"': { chars[i] = '\"'; continue; }

                    //全角标点转换为半角输出
                    case '，': { chars[i] = ','; continue; }
                    case '。': { chars[i] = '.'; continue; }
                    case '｛': { chars[i] = '{'; continue; }
                    case '｝': { chars[i] = '}'; continue; }
                    case '【': { chars[i] = '['; continue; }
                    case '】': { chars[i] = ']'; continue; }
                    case '［': { chars[i] = '['; continue; }
                    case '］': { chars[i] = ']'; continue; }
                    case '！': { chars[i] = '!'; continue; }
                    case '＠': { chars[i] = '@'; continue; }
                    case '＃': { chars[i] = '#'; continue; }
                    case '％': { chars[i] = '%'; continue; }
                    case '＆': { chars[i] = '&'; continue; }
                    case '＊': { chars[i] = '*'; continue; }
                    case '（': { chars[i] = '('; continue; }
                    case '）': { chars[i] = ')'; continue; }
                    case '－': { chars[i] = '-'; continue; }
                    case '＝': { chars[i] = '='; continue; }
                    case '＋': { chars[i] = '+'; continue; }
                    case '～': { chars[i] = '~'; continue; }
                    case '？': { chars[i] = '?'; continue; }
                    case '；': { chars[i] = ';'; continue; }
                    case '：': { chars[i] = ':'; continue; }
                    case '‘': { chars[i] = '\''; continue; }
                    case '’': { chars[i] = '\''; continue; }
                    case '“': { chars[i] = '\"'; continue; }
                    case '”': { chars[i] = '\"'; continue; }
                }

                if ((c >= 'a' && c <= 'z') ||
                    (c >= 'A' && c <= 'Z') ||
                    (c >= '0' && c <= '9'))
                {
                    chars[i] = c;
                    continue;
                }

                if (c >= 'Ａ' && c <= 'Ｚ')
                {
                    chars[i] = (char)(((int)c) - ((int)'Ａ' - (int)'A'));
                    continue;
                }

                if (c >= 'ａ' && c <= 'ｚ')
                {
                    chars[i] = (char)(((int)c) - ((int)'ａ' - (int)'a'));
                    continue;
                }

                if (c >= '０' && c <= '９')
                {
                    chars[i] = (char)(((int)c) - ((int)'０' - (int)'0'));
                    continue;
                }
            }

            return new string(chars);
        }
    }
}
